<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Checkboxes Enter Password Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  input { width: 20px; }
  #subbtn { margin: 0 7px; }
  /* 160 + 160 + 50 + 30 */
  #wrap,
  #sync-task-cover {
    height: 400px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 74px;
    padding: 3px;
  }
  input[id="password"] { width: 120px; }
  input[id="verify"] { width: 120px; }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // set episode interval to 2000 seconds

var SYNONYMS = [
  ['big', 'large', 'huge', 'enormous', 'gigantic'],
  ['small', 'tiny', 'little', 'mini', 'petite'],
  ['red', 'scarlet', 'crimson', 'vermillion'],
  ['happy', 'cheerful', 'joyful', 'gleeful', 'delighted'],
  ['sad', 'unhappy', 'sorrowful', 'miserable', 'tragic'],
  ['angry', 'mad', 'furious', 'irritated'],
  ['evil', 'wicked', 'immoral', 'sinful', 'corrupt', 'depraved'],
  ['wrong', 'incorrect', 'mistaken', 'erroneous'],
  ['real', 'genuine', 'actual'],
  ['strange', 'odd', 'peculiar', 'unusual', 'weird'],
  ['stop', 'cease', 'halt', 'end', 'finish'],
  ['scared', 'terrified', 'panicked', 'fearful', 'frightened', 'afraid'],
  ['quiet', 'calm', 'peaceful', 'serene', 'mild'],
  ['old', 'aged', 'archaic'],
  ['love', 'like', 'adore', 'favor'],
  ['kill', 'murder', 'assassinate'],
  ['keep', 'retain', 'preserve', 'sustain', 'maintain'],
  ['hide', 'conceal', 'camouflage'],
  ['hate', 'despise', 'loathe', 'detest', 'dislike'],
  ['funny', 'humorous', 'amusing', 'comical', 'laughable'],
  ['fat', 'fleshy', 'plump', 'chubby'],
  ['stupid', 'dumb', 'dull', 'unwise'],
  ['delicious', 'savory', 'delectable', 'appetizing'],
  ['cut', 'slice', 'carve', 'chop'],
  ['brave', 'courageous', 'fearless'],
  ['begin', 'start', 'initiate', 'launch'],
  ['answer', 'reply', 'response'],
  ['television', 'televisions', 'TV', 'TVs'],
  ['house', 'home', 'houses', 'homes'],
  ['fire', 'flame', 'fires', 'flames'],
  ['pig', 'pork', 'swine', 'pig'],
  ['rabbit', 'rabbits', 'bunny', 'bunnies'],
  ['car', 'cars', 'automobile', 'automobiles', 'vehicle', 'vehicles'],
  ['water'],
];

var createCheckboxes = function(div){
  var checkboxData = { toclick: {}, clickNames: [] };

  checkboxData.elems = core.randi(2, 7);
  var wordList = SYNONYMS.slice();
  core.shuffle(wordList);
  checkboxData.numToClick = core.randi(1, 6);
  checkboxData.elems = core.randi(Math.max(3, checkboxData.numToClick + 1), 7);
  checkboxData.names = [];
  for(var i=0;i<checkboxData.elems;i++) {
    var chname = core.sample(wordList[i]);
    var label = div.append('label')
    label.append('input').attr('type', 'checkbox').attr('id', 'ch'+i);
    label[0][0].innerHTML += core.sample(wordList[i]);
    div.append('br');
    checkboxData.names.push([i, chname]);
  }
  core.shuffle(checkboxData.names);
  for(var j=0;j<checkboxData.elems;j++) {
    if (j < checkboxData.numToClick) {
      checkboxData.toclick[checkboxData.names[j][0]] = true;
      checkboxData.clickNames.push(checkboxData.names[j][1]);
    } else {
      checkboxData.toclick[checkboxData.names[j][0]] = false;
    }
  }  

  return checkboxData;
}

var genProblem = function() {
  var task_state = -1.0;
  // click-checkboxes-soft
  var div = d3.select('#boxes');
  div.html('');

  var checkboxData = createCheckboxes(div);

  var qstr = checkboxData.clickNames.join(', ');
  if(qstr.length === 0) { qstr = 'nothing'; }

  // enter-password
  d3.select('#password')[0][0].value ='';
  d3.select('#verify')[0][0].value ='';

  var password = ui_utils.generateString(2,6);

  
  // d3.select('#query').html('Select words similar to ' + qstr + ', and then enter the password "<span class="bold">' + password + '</span>" into both text fields and press submit.');
  d3.select('#query').html('Enter the password "<span class="bold">' + password + '</span>" into both text fields and press submit, after selecting words similar to ' + qstr + '.');

  d3.select('#subbtn').on('click', function(){
    // click-checkboxes-soft
    var r = 0;
    for(var i=0;i<checkboxData.elems;i++) {
      var is_checked = d3.select('#ch'+i)[0][0].checked;
      r += is_checked === checkboxData.toclick[i] ? 1.0 : -1.0;
    }
    task_state = r == checkboxData.elems ? 1.0 : -1.0
    console.log('click-checkboxes-soft', task_state)
    // enter-password
    var p = d3.select('#password')[0][0].value;
    var v = d3.select('#verify')[0][0].value;
    var r = (p === password && v === password) ? 1.0 : -1.0;
    console.log('enter-password', r)
    if (task_state == 1.0) {
      core.endEpisode(r, r > 0);
    } else {
      core.endEpisode(-1.0);
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <div id="boxes"></div>
    <br>
    <div id="form">
      <p><label>Password</label><input type="password" id="password"></p>
      <p><label>Verify password</label><input type="password" id="verify"></p>
      <button id="subbtn" class="secondary-action">Submit</button>
    </div>
  </div>
</div>
</body>
</html>
